import csv
import json
import matplotlib.colors as mcolors
import math
import matplotlib.pyplot as plt
import random
import numpy as np
import pandas as pd
from .import networkx_graph as nxg
# import networkx_graph as nxg
colors = list(dict(mcolors.BASE_COLORS, **mcolors.CSS4_COLORS).keys())

# plt.figure(figsize=(40, 40), dpi=60)
# ax = plt.subplot(111)

# ax.xaxis.grid(True, which='major') #x坐标轴的网格使用主刻度
# ax.yaxis.grid(True, which='major') #x坐标轴的网格使用主刻度


def view_map(index):
    with open("D:/session/map/RoadLine.json", 'rb') as load_f:
        RoadLine = json.load(load_f)
    dt = RoadLine[index]["geometry"]["coordinates"]
    if dt == []:
        return
    dtarray = np.array(dt)
    plt.plot(dtarray[:, 1], dtarray[:, 0], color=colors[0], marker='.')

    # with open("./data/RoadPoint.json", 'rb') as load_f:
    #     RoadPoint = json.load(load_f)

    # for rd in RoadPoint:  # len(RoadLine)):
    #     dt = rd["geometry"]["coordinates"]
    #     if dt == []:
    #         continue
    #     dtarray = np.array(dt)
    #     plt.scatter(dtarray[1], dtarray[0],
    #                 color=colors[1], edgecolors='none', s=100)


def GET_my_path(points):
    # C:/Users/tang/Desktop/RHZT/R1/jsontest3/out.csv
    path = 'C:\\Users\\tang\\Desktop\\cmd\\cpp_app\\gen_people_25.csv'
    read_csv = csv.reader(
        open(path, 'r'), dialect='excel')

    _ldd = 0
    pltlins = []
    D5 = []
    for line in read_csv:  # 循环输出csv中的所有数据
        aint = list(int(val) for val in line)
        d3 = []
        for id in range(len(aint)-1):
            d3.append(points[aint[id]])
        # print(d3)
        d4 = np.array(d3)

        # line1 = ax.
        # ax.legend(line1, (str(_ldd),)) # 多加一个逗号
        # line2 = ax.plot(d4[:,1], d4[:,0], color=colors[_ldd+4], marker='*')
        # ax.legend(line2, (str(_ldd),)) # 多加一个逗号
        plt.plot(d4[:, 1], d4[:, 0], color=colors[_ldd+2],
                 marker='.', label=str(_ldd))

        # print(d4[:,1][0],d4[:,0][0])
        plt.scatter(d4[:, 1][0], d4[:, 0][0], c='r', marker='o', s=700)  # 终点
        plt.scatter(d4[:, 1][len(d3)-1], d4[:, 0][len(d3)-1],
                    c='b', marker='o', s=700)  # 起点
        _ldd += 1
        D5.append(d4)
    # print(D5)


def view_my_path(points, path):
    # path='C:\\Users\\tang\\Desktop\\cmd\\cpp_app\\data\\gen_people_25_new.csv' # C:/Users/tang/Desktop/RHZT/R1/jsontest3/out.csv
    # path='C:\\Users\\tang\\Desktop\\cmd\\data\\gen_people_0.csv'
    read_csv = csv.reader(
        open(path, 'r'), dialect='excel')

    _ldd = 0
    pltlins = []
    for line in read_csv:  # 循环输出csv中的所有数据
        aint = list(int(val) for val in line)
        d3 = []
        for id in range(len(aint)):
            d3.append(points[aint[id]])
        # print(d3)
        d4 = np.array(d3)

        # line1 = ax.
        # ax.legend(line1, (str(_ldd),)) # 多加一个逗号
        # line2 = ax.plot(d4[:,1], d4[:,0], color=colors[_ldd+4], marker='*')
        # ax.legend(line2, (str(_ldd),)) # 多加一个逗号
        plt.plot(d4[:, 1], d4[:, 0], color=colors[_ldd+2],
                 marker='.', label=str(_ldd))

        # print(d4[:,1][0],d4[:,0][0])
        plt.scatter(d4[:, 1][0], d4[:, 0][0],
                    color=colors[_ldd+2], marker='o', s=400)  # 终点
        plt.scatter(d4[:, 1][len(d3)-1], d4[:, 0][len(d3)-1],
                    color='b', marker='o', s=100)  # 起点
        _ldd += 1
        # print(d4)

        # plt.scatter(d4[:,1][0], d4[:,0][0],
        #             color=colors[7], edgecolors='none',marker='*')


def view_json_path(round_id, view_id):
    with open('D:/session/session_2/subject_2/team_path_'+str(round_id)+'.json', 'rb') as load_f:
        RoadLine = json.load(load_f)
    # print(RoadLine["car_points"][0]["pass_points"])
    path = []
    # len(RoadLine)):
    for i, rd in enumerate(RoadLine["car_points"][view_id]["pass_points"]):
        path.append([rd["longitude"], rd["latitude"]])
    path = np.array(path)
    plt.plot(path[:, 0], path[:, 1], marker='.')
    plt.scatter(path[:, 0][0], path[:, 1][0],
                color='b', marker='o', s=100)  # 起点

# view_json_path(1,1)
def show_gen_22_path():
    plt.figure(1)
    points, roadLines, roadpoint = nxg.view_maps2(True)
    view_json_path(1, 1)
    plt.figure(2)
    points, roadLines, roadpoint = nxg.view_maps2(True)
    view_json_path(1, 2)
    plt.figure(0)
    points, roadLines, roadpoint = nxg.view_maps2(True)
    view_json_path(1, 0)
    # print(len(points))
    plt.show()



# show_path_by_id(0,1)
